const express = require('express');
const router = express.Router();
const db = require('../libs/db');

/* GET users listing. */
router.get('/', async function (req, res, next) {
  let snippets = await db.getAllSnippets(req.user.id);
  console.log(snippets);
  res.render('me', {
    snippets: snippets
  });
});

router.post('/snippet', async function (req, res, next) {
  let data = Object.assign(req.body, {
    author: req.user.id,
    create_time: Date.now(),
    modified_time: Date.now()
  });
  let r = await db.submitSnippet(data);
  res.send({
    success: !!r
  });
});

router.delete('/snippet', async function (req, res, next) {
  db.removeSnippet(req.body.id).then(d => {
    res.send({});
  });
});

router.get('/snippet', async function (req, res, next) {
  db.getSnippet(req.query.id).then(d => {
    res.send(d);
  });
});


module.exports = router;